********************************************************************************
* EMPLOYMENT EXTRACTION ********************************************************
********************************************************************************
use ${orig}/${employmentfile}.dta, clear
qui: keep employment_sa amr eventdate
qui: reshape wide employment_sa, i(eventdate) j(amr)
qui: sort eventdate
qui: gen idtime = _n
keep if idtime<=60

tsset eventdate
forv ii = 1(1)$N {
qui: gen logemployment`ii' = ln(employment_sa`ii')
reg logemployment`ii' idtime 
qui: mat A = e(b)
qui: gen meanlog`ii' = A[1,2]
qui: predict resid`ii', res
qui: replace resid`ii' = resid`ii'+meanlog`ii'
}

preserve
keep resid* 
export delimited ${inputcali}/employment_ts_estimation.csv, replace novarnames
restore

preserve
keep meanlog* 
duplicates drop
export delimited ${inputcali}/employment_avg_estimation.csv, replace novarnames
restore
********************************************************************************
* COUNTS EXTRACTION ************************************************************
********************************************************************************
use ${orig}/tocalibrateS.dta, clear
egen idtime = group(eventdate)
drop if idtime>60
gen aux = idtime/4
keep if mod(aux,1) == .5
gen logcount=ln(totcount)
by amr, sort: egen meanlog = mean(logcount)
keep amr meanlog
duplicates drop
gen aux = 1
reshape wide meanlog, i(aux) j(amr)
drop aux
export delimited ${inputcali}/count_avg_estimation.csv, replace novarnames
********************************************************************************
* MAX SHARE EXTRACTION *********************************************************
********************************************************************************
use ${orig}/tocalibrateS.dta, clear
egen idtime = group(eventdate)
drop if idtime>60
gen aux = idtime/4
keep if mod(aux,1) == .5
gen share = maxemployment/totemployment
by amr, sort: egen meanshare = mean(share)
keep amr meanshare
duplicates drop
gen aux = 1
reshape wide meanshare, i(aux) j(amr)
drop aux
export delimited ${inputcali}/meanshare_avg_estimation.csv, replace novarnames
********************************************************************************
